﻿<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<link rel="stylesheet" type="text/css" href="/easyui/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="/easyui/themes/icon.css">
	<link rel="stylesheet" type="text/css" href="/easyui/demo.css">
	<link rel="stylesheet" type="text/css" href="/easyui/base.css">

	<link rel="stylesheet" type="text/css" href="/lib/Hui-iconfont/1.0.8/iconfont.css" />

	<script type="text/javascript" src="/easyui/jquery.min.js"></script>
	<script type="text/javascript" src="/easyui/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="/lib/layer/2.4/layer.js"></script>

</head>
<body class="easyui-layout">
<div data-options="region:'center',border:false" style="padding:2px;height: auto">
	<table id="role_list_table" class="easyui-datagrid" title="" style="width:100%;height:100%;padding-bottom: 2px;"
		   data-options="
		   	 rownumbers:true,
		   	 pagination:true,
		   	 singleSelect:true,
		   	 toolbar : '#tb',
		   	 fitColumns : true,
		   	 url:'/list/RoleEntity/setRoleName,setRemarks,setEditQx',
           method:'get',
           onAfterEdit:function(index,row){
           	var role_name = row.roleName;
           	var add_qx = row.addQx;
           	var del_qx = row.delQx;
           	var edit_qx = row.editQx;
           	var cha_qx = row.queryQx;
           	var role_id = row.roleId;
           	var role_marks = row.remarks;
				updateActions(role_name,role_id,role_marks,add_qx,del_qx,edit_qx,cha_qx);
			 },
           showFooter: true"
		>

		<thead>
		<tr>
			<th data-options="field:'roleName',width:90,align:'center',editor:{
					type:'validatebox',
					options:{
						required:true,
						missingMessage:'请填写角色名称'
					}
					},formatter:formatProgress">角色名称</th>
			<th data-options="field:'roleId',width:90,align:'center'" hidden>角色ID</th>
			<th data-options="field:'addQx',width:90,align:'center',editor:{
					type:'combobox',
					options:{
						valueField:'value',
							textField:'text',
							data:[{value:0,text:'无权限'},{value:1,text:'有权限'}],
							editable: false,
							required:true,
							missingMessage:'请选择增权限'
					}
					},formatter:formatCRUD ">增</th>
			<th data-options="field:'delQx',width:90,align:'center',editor:{
					type:'combobox',
					options:{
						valueField:'value',
							textField:'text',
							data:[{value:0,text:'无权限'},{value:1,text:'有权限'}],
							editable: false,
							required:true,
							missingMessage:'请选择删权限'
					}
					},formatter:formatCRUD ">删</th>
			<th data-options="field:'editQx',width:90,align:'center',editor:{
					type:'combobox',
					options:{
						valueField:'value',
							textField:'text',
							data:[{value:0,text:'无权限'},{value:1,text:'有权限'}],
							editable: false,
							required:true,
							missingMessage:'请选择改权限'
					}
					},formatter:formatCRUD ">改</th>
			<th data-options="field:'queryQx',width:90,align:'center',editor:{
					type:'combobox',
					options:{
						valueField:'value',
							textField:'text',
							data:[{value:0,text:'无权限'},{value:1,text:'有权限'}],
							editable: false,
							required:true,
							missingMessage:'请选择查权限'
					}
					},formatter:formatCRUD">查</th>
			<th data-options="field:'remarks',width:90,align:'center',editor:{
					type:'validatebox',
					options:{
						required:true,
						missingMessage:'请填写角色备注'
					}}">角色备注</th>
			<th data-options="field:'rights',width:90,align:'center'" hidden>权限代码</th>
		</tr>
		</thead>
	</table>
</div>


<div id="tb" style="padding:5px;height:auto">
	<a href="#" class="easyui-linkbutton" iconCls="icon-add" id="add">新增</a>
	<a href="#" class="easyui-linkbutton" iconCls="icon-save" id="save">保存</a>
	<a href="#" class="easyui-linkbutton" iconCls="icon-edit" id="edit">编辑</a>
	<a href="#" class="easyui-linkbutton" iconCls="icon-remove" id="delete">删除</a>
	<a href="#" class="easyui-linkbutton" iconCls="icon-man" id="assign">分配权限</a>
</div>


<script type="text/javascript">

	//角色名称-格式化
	function formatProgress(value){
		if (value){
			return value;
		} else {
			return '';
		}
	}
	//增删改查-格式化
	function formatCRUD(value){
        if('1'==value){
            return '<i class="Hui-iconfont">&#xe6a7;</i>'
        }else if('0'==value){
            return '<i class="Hui-iconfont">&#xe6a6;</i>'
        }
	}

</script>

<script type="text/javascript">

	//分配权限
	$("#assign").click(function(){
		var row = $('#role_list_table').datagrid('getSelected');
		if(!row){
			$.messager.show({
				title:'提示',
				msg:'请选择需要分配权限的角色',
				timeout:3000,
				showType:'slide',
			});
		}else{
			layer.open({
				type: 2,
				title: false,
				closeBtn: 0,
				shadeClose: true,
				area: ['757px', '300px'],
				skin: 'demo-class',
				content: '/admin/admin-role-assign?rights='+row.rights+'&role_id='+row.roleId
			});
		}
	})

	//编辑
	$("#edit").click(function () {
		var row = $('#role_list_table').datagrid('getSelected');
		if (row){
			var index = $('#role_list_table').datagrid('getRowIndex', row);
            $('#role_list_table').datagrid('selectRow',index);
            $('#role_list_table').datagrid('beginEdit',index);
		} else {
            $.messager.show({
                title:'提示',
                msg:'请选择需要编辑的角色',
                timeout:3000,
                showType:'slide',
            });
		}

	})

	//保存
	$("#save").click(function () {
		var row = $('#role_list_table').datagrid('getSelected');
		if (row){
			var index = $('#role_list_table').datagrid('getRowIndex', row);
		} else {
			index = 0;
		}
		$('#role_list_table').datagrid('endEdit',index);
	})

	//新增
	$("#add").click(function () {
		var row = $('#role_list_table').datagrid('getSelected');
		if (row){
			var index = $('#role_list_table').datagrid('getRowIndex', row);
		} else {
			index = 0
		}
		$('#role_list_table').datagrid('insertRow', {index: index,row:{chaQx:'1'}});
		$('#role_list_table').datagrid('selectRow',index);
		$('#role_list_table').datagrid('beginEdit',index);
	})

	//删除
	$("#delete").click(function () {
		var row = $('#role_list_table').datagrid('getSelected');
		if(!row){
			$.messager.show({
				title:'提示',
				msg:'请选择需要删除的角色',
				timeout:3000,
				showType:'slide',
			});
		}else{
			$.messager.confirm('删除信息','确定删除用户:'+row.roleName+"吗?",function(r){
				if (r){
					$.ajax({
						url : '/admin-role-delete',
						data : {roleID : row.roleId},
						async : true,
						success : function(){
							$('#role_list_table').datagrid('reload');
						}
					});
				}
			});
		}
	})
</script>
<script>
	function updateActions(role_name,role_id,role_marks,add_qx,del_qx,edit_qx,cha_qx){
		var url = '/admin-role-list-insert';
		//修改
		if(role_id!=null&&role_id!=''){
			url = '/admin-role-list-update';
		}
		$.ajax({
			url : url,
			data : {role_name : role_name,role_id:role_id,add_qx : add_qx,del_qx:del_qx,edit_qx:edit_qx,cha_qx:cha_qx,role_marks:role_marks,tm:new Date()},
			async : true,
			type : 'post',
			success :function () {
				$('#role_list_table').datagrid('reload');
			},
			error : function () {
				alert('error');
			}
		})
	}
</script>
</body>
</html>